import {uuid} from './utiles'
import {point} from './entity/point.js'
class polygonmark {

  constructor(options) {
    this._positions = []
    this.id = uuid() + '|plotmark'
    this.viewer = window.viewer
    this.options = {
      name: "多边形",
      id: this.id,
      polygon: {
        show: true,
        hierarchy: new Cesium.PolygonHierarchy([]),
        // height: 0,
        material: Cesium.Color.CYAN.withAlpha(0.0),
        outline: true,
        outlineColor: Cesium.Color.BLACK,
        // classificationType: Cesium.ClassificationType.BOTH
        ...options.options
      },
      polyline: {
        positions: this._positions,
        width: 3,
        material: Cesium.Color.BLUE.withAlpha(0.5),
        // clampToGround: true,
        ...options.options
      },
    };
    this.entity = null
    this._init()
    this.editpoint = []

  }

  type = 'polygonmark'
  name = '多边形'

  /**
   * 获取pos
   */
  get positions() {
    return this._positions
  }

  setwgs84pos(pos){
    let respos = []
    pos.map((item, i)=>{
      respos.push(Cesium.Cartesian3.fromDegrees(item.lng, item.lat, item.alt))
    })
    this.setPositions(respos)
  }

  flyto(){
    // window.viewer.camera.flyTo({
    //   destination: Cesium.Cartesian3.fromDegrees(112.98242, 22.87485, 100000),
    //   orientation: {
    //     heading: Cesium.Math.toRadians(0), // 水平偏角，默认正北 0
    //     pitch: Cesium.Math.toRadians(-90), // 俯视角，默认-90，垂直向下
    //     roll: 0, // 旋转角
    //   },
    // });
    var heading = Cesium.Math.toRadians(0.0);
    var pitch = Cesium.Math.toRadians(-90.0);
    var roll = Cesium.Math.toRadians(0);
    window.viewer.flyTo(this.entity, {
      duration: 0.2,
      offset: new Cesium.HeadingPitchRange(heading, pitch, roll)
    });
  }

  /**
   * 获取经纬度坐标
   */
  get wgs84pos(){
    let c = null
    let res = []
    this._positions.map((item, i)=>{
      c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(item)
      if (c) {
        res.push({
          lng: Cesium.Math.toDegrees(c.longitude),
          lat: Cesium.Math.toDegrees(c.latitude),
          alt: c.height
        })
      }
    })
    return res
  }

  /**
   * 创建编辑控制点
   * @returns 
   */
  createPoints(){
    this.editpoint = []
    this._positions.map((item, i)=>{
      this.editpoint.push(new point({
        id: uuid() + '|editpoint|' + i,
        positions: item,
      }))
    })
    return this.editpoint
  }

  /**
   * 设置pos
   * @param {*} pos 
   */
  setPositions(pos) {
    this._positions = pos
  }


  _init(params) {
    var _self = this;
    var _update = function () {
      return new Cesium.PolygonHierarchy(_self.positions);
    };
    var _update1 = function () {
      return [..._self.positions, _self.positions[0]]
    };
    // 实时更新polyline.positions
    this.options.polygon.hierarchy = new Cesium.CallbackProperty(
      _update,
      false
    );
    this.options.polyline.positions = new Cesium.CallbackProperty(
      _update1,
      false
    );

    this.entity = new Cesium.Entity(this.options)

    // this.createPoints = createPoints

  }
}

export default polygonmark